package com.iwe.crm.workbench.controller;

import com.iwe.crm.base.bean.ResultVo;
import com.iwe.crm.base.exception.CrmException;
import com.iwe.crm.base.util.CommonUtil;
import com.iwe.crm.user.bean.User;
import com.iwe.crm.workbench.bean.Activity;
import com.iwe.crm.workbench.bean.Clue;
import com.iwe.crm.workbench.bean.ClueActivityRelation;
import com.iwe.crm.workbench.bean.Tran;
import com.iwe.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/clue")
public class ClueController {


    @Autowired
    private ClueService clueService;

    //查询当前线索未关联的市场活动
    @GetMapping("/queryActivies")
    public List<Activity> queryActivies(String name,String clueId){
        return clueService.queryActivies(name,clueId);
    }

    //线索关联市场活动
    @GetMapping("/bind")
    public ResultVo bind(String clueId, String aids){
       ResultVo resultVo = new ResultVo();
       try{
           clueService.bind(clueId,aids);
           resultVo.setOk(true);
           resultVo.setMessage("关联市场活动成功");
       }catch (CrmException e){

       }
       return resultVo;
    }

    //刷新关联的市场活动
    @GetMapping("/updateRelation")
    public List<Activity> updateRelation(String clueId){
        List<Activity> activities = null;
        try{
            activities = clueService.updateRelation(clueId);
        }catch (CrmException e){

        }
        return activities;
    }

    //解绑
    @GetMapping("/unbind")
    public ResultVo unbind(ClueActivityRelation clueActivityRelation){
        ResultVo resultVo = new ResultVo();
        try{
            clueService.clueActivityRelation(clueActivityRelation);
            resultVo.setOk(true);
            resultVo.setMessage("解绑成功");
        }catch (CrmException e){

        }
        return resultVo;
    }

    //根据主键查询线索信息
    @GetMapping("/queryById")
    public Clue queryById(String id){
        return clueService.queryById(id);
    }

    //线索转换
    @PostMapping("/convert")
    public ResultVo convert(String clueId, HttpSession session,
                            String isCreateTransaction, Tran tran){
        ResultVo resultVo = new ResultVo();
        User user = CommonUtil.getCurrentUser(session);
        try{
            clueService.convert(clueId,user,isCreateTransaction,tran);
            resultVo.setOk(true);
            resultVo.setMessage("线索转换成功");
        }catch (CrmException e){
            e.printStackTrace();
        }
        return resultVo;
    }

    //模糊查询当前线索已经关联过的市场活动
    @GetMapping("/queryRelatedActivity")
    public ResultVo queryRelatedActivity(String id,String name){
        ResultVo resultVo = new ResultVo();
        try{
            List<Activity> activities =
                    clueService.queryRelatedActivity(id, name);
            resultVo.setT(activities);
            resultVo.setOk(true);
        }catch (CrmException e){
            e.printStackTrace();
            resultVo.setMessage(e.getMessage());
        }
        return resultVo;
    }
}
